package net.wangds.jpa.conv;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.function.Function;
import java.util.function.Supplier;

@NoArgsConstructor
@AllArgsConstructor
public class PageRequestConv<T> implements Converter<T, PageRequest> {

    @Getter
    @Setter
    private Function<T, Integer> pageIndexFac;

    @Setter
    @Getter
    private Function<T, Integer> pageSizeFac;

    @Setter
    @Getter
    private Supplier<Sort> sortFac;

    @Override
    public PageRequest convert(T source) {
        return PageRequest.of(pageIndexFac.apply(source), pageSizeFac.apply(source),sortFac.get() );
    }
}
